package com.grid;


import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 与具体ORM实现无关的分页参数及查询结果封装.
 * 注意所有序号从1开始.
 *
 * @param <?> Page中记录的类型.
 * @author calvin
 */
public class Page {
    /*
    *       Extjs     分页的相关变量
     *
    * */
    //分页大小
    private int limit;
    private int start = 0;
    //排序函数和排序方式
    private String sort;
    //当前也
    private int page;

    public String getParameter(String name) {
        return this.request == null ? null : this.request.getParameter(name);
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    //请求的request
    private HttpServletRequest request;

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.pageSize = (limit == 0) ? 20 : limit;
        this.limit = limit;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        if (!StringUtils.isEmpty(sort)) {
            //需要把[] 替换掉  如果有JSONObject不能解析
            //    JSONObject jsonObj = JSONObject.fromObject(sort.toString().replace('[', ' ').toString().replace(']', ' '));
            Map<String, String> map = JSON.parseObject(sort, Map.class);
            this.orderBy = map.get("property");
            this.setOrderDir(map.get("direction"));
        }
        this.sort = sort;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.pageNo = page;
        this.page = page;
    }

    /**
     * 获得当前页的第一条记录序号，从0开始,默认为0.
     */
    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    //-- 分页参数 --//
    //当前页
    private int pageNo = 1;
    //分页大小
    private int pageSize = 20;
    //排序字段
    private String orderBy;
    //排序方向
    private String orderDir;
    //前台页面要查询的字段
    private String params;

    private String fieldList;
    //要分页的类
    private String tableName;

    public String getFieldList() {
        return fieldList;
    }

    public void setFieldList(String fieldList) {
        this.fieldList = fieldList;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    //where条件
    private String where;
    //where条件里的参数
    private Map<String, Object> whereParams;

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
    	if(where ==null){
    		 this.where = null;
    		 this.whereParams = null;  
          }
          else{
        	  if(this.where!=null)
                  this.where+=" and "+where;
              else{
                  this.where = where;
                
          }
          }
    }

    public Map<String, Object> getWhereParams() {
        return whereParams;
    }

    public void setWhereParams(Map<String, Object> whereParams) {
        if(this.whereParams==null)
           this.whereParams = whereParams;
        else
        {
            for (Map.Entry<String, Object> m : whereParams.entrySet()) {
                this.whereParams.put( m.getKey(), m.getValue());
            }

        }
    }

    public String getParams() {
        return params;
    }

    public void setParams(String params) {
        this.params = params;
    }

    //-- 构造函数 --//
    public Page() {
        super();
    }

    public void setOrderDir(String orderDir) {
        this.orderDir = orderDir;
    }

    public String getOrderDir() {
        return orderDir;
    }


    //-- 访问查询参数函数 --//

    /**
     * 获得当前页的页号,序号从1开始,默认为1.
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * 设置当前页的页号,序号从1开始,低于1时自动调整为1.
     */
    public void setPageNo(final int pageNo) {
        this.pageNo = (pageNo < 1) ? 1 : pageNo;
    }


    /**
     * 获得每页的记录数量,默认为20.
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 设置每页的记录数量,低于1时自动调整为20.
     */
    public void setPageSize(final int pageSize) {
        this.pageSize = (pageSize < 1) ? 20 : pageSize;

    }

    /**
     * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
     */
    public int getFirst() {
        return ((pageNo - 1) * pageSize) + 1;
    }

    /**
     * 获得排序字段,无默认值.多个排序字段时用','分隔.
     */
    public String getOrderBy() {
        return orderBy;
    }

    /**
     * 设置排序字段,多个排序字段时用','分隔.
     */
    public void setOrderBy(final String orderBy) {
        this.orderBy = orderBy;
    }

    /**
     * 是否还有上一页.
     */
    public boolean isHasPre() {
        return (pageNo - 1 >= 1);
    }

    /**
     * 取得上页的页号, 序号从1开始.
     * 当前页为首页时返回首页序号.
     */
    public int getPrePage() {
        if (isHasPre()) {
            return pageNo - 1;
        } else {
            return pageNo;
        }
    }

    public String getHQL() throws Exception {
        if (tableName == null) {
            throw new Exception("没有设置表");
        } else {
            String strOrder = this.orderBy != null ? " order by " + this.orderBy + " " + this.orderDir : "";
            return  (this.fieldList==null?"":"select "+this.fieldList) + " from " + tableName + " " + ((this.where != null) ? " where " + this.where + "" : "") + strOrder;
        }
    }

    public String getHQLTotalCount() {
        String strOrder = this.orderBy != null ? " order by " + this.orderBy + " " + this.orderDir : "";
        return "select count(*)  from " + tableName + ((this.where != null) ? " where " + this.where + "" : "") + strOrder;
    }

}